package servlet;

import model.Type;
import service.TypeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/type_add")
public class AdminTypeAddServlet extends HttpServlet {
    private TypeService tService = new TypeService();

    // 处理 POST 请求（添加类目）
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name"); // 获取类目名称
        tService.insert(new Type(name)); // 插入新类目
        request.setAttribute("msg", "类目添加成功！");
        request.getRequestDispatcher("/admin/type_list").forward(request, response); // 返回类目列表
    }

    // 处理 GET 请求（跳转到添加页面）
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/admin/type_add.jsp").forward(request, response);
    }
}